import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login'
import Home from '../components/Home'
import Welcome from '../components/Welcome'
import Users from '../views/system/users/Users'
import Project from '../views/system/project/Project'
import Interface from '../views/task/interface/Interface'
import Case from '../views/task/interface/Case'
import Tasks from '../views/task/tasks/Tasks'
import Dict from '../views/system/dict/Dict'
import Report from '../views/task/tasks/Report'
import Permission from '../views/system/permission/Permission'
import Role from '../views/system/role/Role'
import Test from '../components/Test'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login,
    meta: {
      title: '登录页面'
    }
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: Welcome
      },
      {
        path: '/users',
        component: Users
      },
      {
        path: '/project',
        component: Project
      },
      {
        path: '/interface',
        component: Interface
      },
      {
        path: '/interface/case',
        component: Case
      },
      {
        path: '/tasks',
        component: Tasks
      },
      {
        path: '/report',
        component: Report
      },
      {
        path: '/dict',
        component: Dict
      },
      {
        path: '/permission',
        component: Permission
      },
      {
        path: '/role',
        component: Role
      },
      {
        path: '/test',
        component: Test
      }
    ]
  },
]

const router = new VueRouter({
  routes
})

import Cookies from 'js-cookie'

// 路由导航守护
router.beforeEach((to, from, next) => {
  if (to.path === '/login') next()
  const token = Cookies.get('Authorization')
  if (!token) return next('/login')
  next()
})

export default router
